package com.stylefeng.guns.modular.train.service.impl;

import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.persistence.model.MentoringRelation;
import com.stylefeng.guns.common.persistence.dao.MentoringRelationMapper;
import com.stylefeng.guns.common.persistence.model.MentoringRelationPoints;
import com.stylefeng.guns.common.persistence.model.QualityGradeRule;
import com.stylefeng.guns.modular.system.service.IQualityGradeRuleService;
import com.stylefeng.guns.modular.train.service.IMentoringRelationPointsService;
import com.stylefeng.guns.modular.train.service.IMentoringRelationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rockjh
 * @since 2018-01-29
 */
@Service
public class MentoringRelationServiceImpl extends ServiceImpl<MentoringRelationMapper, MentoringRelation> implements IMentoringRelationService {

    @Autowired
    private IMentoringRelationPointsService mentoringRelationPointsService;

    @Autowired
    private MentoringRelationMapper mentoringRelationMapper;

    @Autowired
    private IQualityGradeRuleService qualityGradeRuleService;

    @Override
    public void updateByMentoringRelationChange(MentoringRelation mentoringRelation,String operation) {

        if(("1".equals(mentoringRelation.getExamineResultCode()) &&
                (Const.OPERATION_ADD.equals(operation) || Const.OPERATION_DELETE.equals(operation))) ||
                Const.OPERATION_UPDATE.equals(operation)){
            String masterEmpCode=mentoringRelation.getMasterEmpCode();
            String year=mentoringRelation.getYear();
            String apprenticeEmpCode=mentoringRelation.getApprenticeEmpCode();
            QualityGradeRule masterQualityGradeRule = qualityGradeRuleService.getByGradeType(Const.TEACHER).get(0);
            QualityGradeRule bothQualityGradeRule = qualityGradeRuleService.getByGradeType(Const.MASTER).get(0);
            List<MentoringRelation> masterMentoringRelations=findByMasterEmpCodeYear(masterEmpCode,year);
            double masterIntegral=0;
            double apprenticeIntegral=0;
            //师傅积分
            for(MentoringRelation item :masterMentoringRelations){
                if("1".equals(item.getExamineResultCode())){
                    masterIntegral+=bothQualityGradeRule.getIntegral();
                }
                if(masterIntegral>=masterQualityGradeRule.getIntegral()){
                    break;
                }
            }
            MentoringRelationPoints masterMentoringRelationPoints=mentoringRelationPointsService.findByEmpYearIntegralType(masterEmpCode,year, Const.MENTORING_INTEGRAL_TYPE_MASTER_CODE);
            if(masterMentoringRelationPoints==null){
                masterMentoringRelationPoints=new MentoringRelationPoints();
                masterMentoringRelationPoints.setCreateDate(new Date());
                masterMentoringRelationPoints.setEmpCode(masterEmpCode);
                masterMentoringRelationPoints.setEmpName(mentoringRelation.getMasterEmpName());
                masterMentoringRelationPoints.setIntegralType(Const.MENTORING_INTEGRAL_TYPE_MASTER_NAME);
                masterMentoringRelationPoints.setIntegralTypeCode(Const.MENTORING_INTEGRAL_TYPE_MASTER_CODE);
                masterMentoringRelationPoints.setYear(year);
                masterMentoringRelationPoints.setDataOrigin(Const.IMPORT_TYPE_SYS);
                mentoringRelationPointsService.insert(masterMentoringRelationPoints);
            }

            if(masterIntegral==0){
                mentoringRelationPointsService.deleteById(masterMentoringRelationPoints.getId());
            }else {
                masterMentoringRelationPoints.setIntegral(masterIntegral);
                mentoringRelationPointsService.updateById(masterMentoringRelationPoints);
            }

            //徒弟积分
            MentoringRelation apprenticeMentoringRelations=findByApprenticeEmpCodeYear(apprenticeEmpCode,year);
            if(apprenticeMentoringRelations!=null && "1".equals(apprenticeMentoringRelations.getExamineResultCode())){
                apprenticeIntegral=Const.OPERATION_DELETE.equals(operation)?0:bothQualityGradeRule.getIntegral();
            }
            MentoringRelationPoints apprenticeMentoringRelationPoints=mentoringRelationPointsService.findByEmpYearIntegralType(apprenticeEmpCode,year, Const.MENTORING_INTEGRAL_TYPE_APPRENTICE_CODE);
            if(apprenticeMentoringRelationPoints==null){
                apprenticeMentoringRelationPoints=new MentoringRelationPoints();
                apprenticeMentoringRelationPoints.setCreateDate(new Date());
                apprenticeMentoringRelationPoints.setEmpCode(apprenticeEmpCode);
                apprenticeMentoringRelationPoints.setEmpName(mentoringRelation.getApprenticeEmpName());
                apprenticeMentoringRelationPoints.setIntegralType(Const.MENTORING_INTEGRAL_TYPE_APPRENTICE_NAME);
                apprenticeMentoringRelationPoints.setIntegralTypeCode(Const.MENTORING_INTEGRAL_TYPE_APPRENTICE_CODE);
                apprenticeMentoringRelationPoints.setYear(year);
                apprenticeMentoringRelationPoints.setDataOrigin(Const.IMPORT_TYPE_SYS);
                apprenticeMentoringRelationPoints.setIntegral(bothQualityGradeRule.getIntegral());
                mentoringRelationPointsService.insert(apprenticeMentoringRelationPoints);
            }else {
                if(apprenticeIntegral==0){
                    mentoringRelationPointsService.deleteById(apprenticeMentoringRelationPoints.getId());
                }else {
                    apprenticeMentoringRelationPoints.setIntegral(apprenticeIntegral);
                    mentoringRelationPointsService.updateById(apprenticeMentoringRelationPoints);
                }
            }

        }
    }

    @Override
    public List<MentoringRelation> findByMasterEmpCodeYear(String masterEmpCode, String year) {
        return mentoringRelationMapper.findByMasterEmpCodeYear(masterEmpCode,year);
    }

    @Override
    public MentoringRelation findByApprenticeEmpCodeYear(String apprenticeEmpCode, String year) {
        return mentoringRelationMapper.findByApprenticeEmpCodeYear(apprenticeEmpCode,year);
    }

}
